function cancel(e) {
  if (e.preventDefault) {
    e.preventDefault();
  }
  return false;
}

function CardDrag(ev)
{
    // drag only id its your turn and not animation
    if ( (ISMYTURN) && (ArrayStartIndex < 0 ) ) {
        ev.dataTransfer.setData("Text",ev.target.id);
    }
} 

function allowDrop(event)
{
    event.preventDefault();
}



// make player BBoxes to be dropable

for (var i=0;i<4;i++)
{
    for (var j=0;j<3;j++)
    {
        var drop = document.querySelector( '#p1_board_' + i.toString() + j.toString() );

        // Tells the browser that we *can* drop on this target

        addEvent(drop, 'dragenter', cancel);
        addEvent(drop, 'dragover', cancel);

        //addEvent(drop, 'dragstart', function (e) {
//                e.dataTransfer.setData('text/plain', '1123');
//            });

        addEvent(drop, 'drop', function (event) {
            event.preventDefault();
            if (event.preventDefault) 
                event.preventDefault(); // stops the browser from redirecting off to the text.

            var card_id = event.dataTransfer.getData('Text'); // card(x) ,Ex: "card1"
            
            // check if user dragged a card (and not anything else), and it IS User Turn
            if ( card_id.startsWith("card") && card_id.length == 5 && ISMYTURN) {
                
                card_id = card_id.charAt(4) ; // get only the card number from the string
                
                // now we need to get the i,j of the dropped cell in board
                var DroppedCellName = event.target.id;  // get the full cell name,Ex: "p1_board_30"
                //Check if we dropped in Board Cell
                if ( DroppedCellName.startsWith("p1_board_") && DroppedCellName.length == 11 ) {
                    //Parse the 2 digits to be i,j pos in board
                    var i,j;
                    i = DroppedCellName.charAt(9);
                    j = DroppedCellName.charAt(10);

                    // now we have: card_id , i , j
                    // Check if the cell is empty &&
                    //   first time to place soldier &&
                    //   not in the midle of the proccess ClickHereClickThere
                    if ( (PlayerBoard[i][j]["Type"] == "xxx")  && (!NextCommand.PlaceSoldier) && (ClickHere_CardID==null) ) {
                        //OK, we can put card in this empty cell
                        var cType = parseInt(Cards[card_id]["Card_id"]);    //get card num by int
                        PlayerBoard[i][j]["LifeElement"].innerHTML = CardsDefs[cType]["HP"];
                        PlayerBoard[i][j]["ImageSpanElement"].innerHTML ="<img src='images/game/soldiers/00" + cType + "/1.png' onclick='UnplaceSoldier();'>" ;
                        //now remove card from deck, bu save it first if user unplace soldier to restore card
                        LastCardElement =  Cards[card_id]["Element"].innerHTML;
                        LastCardID = card_id;
                        Cards[card_id]["Element"].innerHTML = "";
                        NextCommand.PlaceSoldier = true;
                        PlaceSoldierType.Card = card_id;
                        PlaceSoldierType.i = i;
                        PlaceSoldierType.j = j;
                    }
                }
            }
            
            
            //this.innerHTML = event.dataTransfer.getData('Text') + "," + event.target.id  ;//+ '<br><img src="images/game/ch1.png">';//'<p>' + e.dataTransfer.getData('Text') + '</p>';
            
            return false;
        });

    }
}

